package com.tuanzi.loan.business.repository.statistics.impl;

import com.tuanzi.loan.business.repository.statistics.WholeProcessIndependentRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.statistics.WholeProcessIndependentsearchRequest;
import com.tuanzi.loan.vo.response.statistics.WholeProcessIndependentsearchResponse;
import org.apache.commons.lang3.StringUtils;
import org.jooq.Condition;
import org.jooq.SelectQuery;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.sql.Date;
import java.util.Objects;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanIndependentProcess.LOAN_INDEPENDENT_PROCESS;
@Repository
public class WholeProcessIndependentRepositoryImpl extends JOOQBaseRepositoryPlus implements WholeProcessIndependentRepository {
    @Override
    public Page<WholeProcessIndependentsearchResponse> list(WholeProcessIndependentsearchRequest searchRequest, Pageable pageable) {
        Condition condition = DSL.trueCondition();
        if (StringUtils.isNotBlank(searchRequest.getActivityName())){
            condition = condition.and(LOAN_INDEPENDENT_PROCESS.ACTIVITYNAME.eq(searchRequest.getActivityName()));
        }
        if (Objects.nonNull(searchRequest.getStartDate())){
            condition = condition.and(LOAN_INDEPENDENT_PROCESS.TIME.ge(new Date(searchRequest.getStartDate().getTime())));
        }
        if (Objects.nonNull(searchRequest.getEndDate())){
            condition = condition.and(LOAN_INDEPENDENT_PROCESS.TIME.le(new Date(searchRequest.getEndDate().getTime())));
        }
        if (Objects.nonNull(searchRequest.getPrdId())){
            condition = condition.and(LOAN_INDEPENDENT_PROCESS.PRDID.eq(searchRequest.getPrdId()));
        }
        SelectQuery sql =dslContext.select(LOAN_INDEPENDENT_PROCESS.fields())
                .from(LOAN_INDEPENDENT_PROCESS)
                .where(condition)
                .orderBy(LOAN_INDEPENDENT_PROCESS.TIME.desc()).getQuery();
        return search(sql,pageable,record -> {
            WholeProcessIndependentsearchResponse response = new WholeProcessIndependentsearchResponse();
            response.setId(record.get(LOAN_INDEPENDENT_PROCESS.ID));
            response.setTime(record.get(LOAN_INDEPENDENT_PROCESS.TIME));
            response.setActivityName(record.get(LOAN_INDEPENDENT_PROCESS.ACTIVITYNAME));
            response.setWaitMoney(record.get(LOAN_INDEPENDENT_PROCESS.WAITMONEY));
            response.setSignUv(record.get(LOAN_INDEPENDENT_PROCESS.SIGNUV));
            response.setRealNameComplete(record.get(LOAN_INDEPENDENT_PROCESS.REALNAMECOMPLETE));
            response.setPrdId(record.get(LOAN_INDEPENDENT_PROCESS.PRDID));
            response.setOperaCerTyComplete(record.get(LOAN_INDEPENDENT_PROCESS.OPERACERTYCOMPLETE));
            response.setFirstInnerOrder(record.get(LOAN_INDEPENDENT_PROCESS.FIRSTINNERORDER));
            response.setCompleteInfoComplete(record.get(LOAN_INDEPENDENT_PROCESS.COMPLETEINFOCOMPLETE));
            response.setCommitSuccessUv(record.get(LOAN_INDEPENDENT_PROCESS.COMMITSUCCESSUV));
            response.setBioAssayComplete(record.get(LOAN_INDEPENDENT_PROCESS.BIOASSAYCOMPLETE));
            response.setBioAsSay(record.get(LOAN_INDEPENDENT_PROCESS.BIOASSAY));
            response.setBindCardComplete(record.get(LOAN_INDEPENDENT_PROCESS.BINDCARDCOMPLETE));
            response.setApproveUv(record.get(LOAN_INDEPENDENT_PROCESS.APPROVEUV));
            response.setSignCompleteUv(record.get(LOAN_INDEPENDENT_PROCESS.SIGNCOMPLETEUV));
            response.setRealNameNext(record.get(LOAN_INDEPENDENT_PROCESS.REALNAMENEXT));
            response.setPushOrderUv(record.get(LOAN_INDEPENDENT_PROCESS.PUSHORDERUV));
            response.setOcrComplete(record.get(LOAN_INDEPENDENT_PROCESS.OCRCOMPLETE));
            response.setFaceuv(record.get(LOAN_INDEPENDENT_PROCESS.FACEUV));
            response.setBackuv(record.get(LOAN_INDEPENDENT_PROCESS.BACKUV));
            response.setRealName(record.get(LOAN_INDEPENDENT_PROCESS.REALNAME));
            response.setOperaCerTy(record.get(LOAN_INDEPENDENT_PROCESS.OPERACERTY));
            response.setBindCard(record.get(LOAN_INDEPENDENT_PROCESS.BINDCARD));
            response.setCompleteInfo(record.get(LOAN_INDEPENDENT_PROCESS.COMPLETEINFO));
            return response;
        });
    }

    @Override
    protected TableImpl getTable() {
        return LOAN_INDEPENDENT_PROCESS;
    }
}
